﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TjZheng.SSO.Business;
using TjZheng.SSO.DataEntity;
using TjZheng.Cache;
using TjZheng.Framework;
using TjZheng.WebApi;
using TjZheng.SSO.Process;

namespace TjZheng.SSO.Web.Controllers
{
    public class SSOApiController : TjZhengApiController
    {
        public UserProcess CurrentUserProcess { get; private set; }

        public virtual UserProcess GetUserProcess()
        {
            return new UserProcess();
        }

        public SSOApiController()
        {
            this.CurrentUserProcess = GetUserProcess();
        }

        // GET api/sso
        public virtual InvokeResult Get()
        {
            InvokeResult objInvokeResult = new InvokeResult();
            return objInvokeResult;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual InvokeResult Get(string id)
        {
            return CurrentUserProcess.GetUserInfo(id);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="objUserInfo"></param>
        /// <returns></returns>
        public virtual InvokeResult Post([FromBody]UserInfo objUserInfo)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objUserInfo == null)
            {
                objInvokeResult.ResultCode = "UserInfoNull";
                objInvokeResult.ResultMessage = "对不起，用户名或者用户密码不能为空";
                return objInvokeResult;
            }
            return CurrentUserProcess.LoginIn(objUserInfo.UserName, objUserInfo.Password);
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="id"></param>
        /// <param name="objUserInfo"></param>
        /// <param name="repassword"></param>
        /// <returns></returns>
        public virtual InvokeResult Put(int id, [FromBody]UserInfo objUserInfo, string rePassword)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objUserInfo.UserName.IsNull())
            {
                objInvokeResult.ResultCode = "UserNameNull";
                objInvokeResult.ResultMessage = "对不起，用户名不能为空";
                return objInvokeResult;
            }
            return CurrentUserProcess.Register(objUserInfo.UserName, objUserInfo.Password, rePassword);
        }

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual InvokeResult Delete(string id)
        {
            return CurrentUserProcess.LoginOut(id);
        }
    }
}
